package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @Author ZhangXiXi
 * @Description TODO
 * @date 2021/2/4 12:03
 * @Version 1.0
 */
@Service
@Slf4j
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.1 检查参数
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID );
        }
        //1.2 检查分页
        dto.checkParam();
        //3 模糊查询+分页
       IPage page = new Page(dto.getPage(),dto.getSize());
       //频道名称模糊查询
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(dto.getName())){

            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        page=page(page,lambdaQueryWrapper);
        //3 结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        System.out.println("2222");
        System.out.println(((PageResponseResult) responseResult).getCurrentPage());
        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //检查参数
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID );
        }
        //保存
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        //检查参数
        if(adChannel==null||adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID );
        }
        //修改
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //检查参数
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID );
        }
        //查询频道
        AdChannel adChannel=getById(id);
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //频道是否有效
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效,不能删除");
        }
        //删除
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
